package com.gopay.bis.common.util;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期操作类
 * 
 * @DateUtils.java
 * @author yaobo
 * @2012-4-19 下午4:54:48 www.gopay.com.cn Inc.All rights reserved.
 */
public class DateUtils {
    public final static String DEFAULT_DATE_FORMAT = "yyyyMMddhhssmm";
    public final static String [] PARSE_PATTERN_ARRAY = {"yyyy-MM-dd HH:mm:ss","yyyyMMddHHmmss","yyyyMMdd HH:mm:ss"};
    

    /**
     * 格式化时间，如果date为null则返回当前时间
     * 
     * @param date
     * @param format
     * @return
     */
    public static String format(String date, String format) {
        SimpleDateFormat s = new SimpleDateFormat(format);
        try {
            return String.valueOf(s.parse(date).getTime());// 时间戳
        } catch (ParseException e) {
            e.printStackTrace();
            return String.valueOf(new Date().getTime());
        }
    }

    /**
     * 格式化日期格式
     * 
     * @param date
     * @param mask
     * @return
     */
    public static final String date2string(Date date, String style) {
        SimpleDateFormat sdf = new SimpleDateFormat(style);
        return sdf.format(date);
    }

    /**
     * 获取本地日期时间.
     * 
     * @return 本地日期时间yyyyMMddHHmmss
     */
    public static String getLocalFullDateTime14() {
        return date2string(new Date(), DEFAULT_DATE_FORMAT);
    }
    /**
     * 获取现在时间Strng
     * @param mask
     * @return
     */
    public static final String now2string(String mask){
        return date2string(new Date(), mask);
    }
    
	public static final Date now(){
		return new Date(System.currentTimeMillis());
	}
	
	   /**
     * 获取系统当前时间
     * 
     * @return
     */
    public static Date getSystemDate() {
        long d = System.currentTimeMillis();
        Date date = new Date(d);

        return date;
    }
	
    /**
     * 判断是否为同一天
     * @param cal1
     * @param cal2
     * @return
     */
	public static boolean isSameDay(Calendar cal1, Calendar cal2) {
        if (cal1 == null || cal2 == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
                cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
    }
	
	/**
	 * 判断是否为同一天
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameDay(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        return isSameDay(cal1, cal2);
    }
	
	/**
	 * 判断是否为当日
	 * @param str
	 * @return
	 */
	public static boolean isToday(String str) {
	    try {
            return isSameDay(parseDate(str,PARSE_PATTERN_ARRAY), new Date());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
	   
	}
	
	/**
	 * 判断是否为当日
	 * @param date
	 * @return
	 */
	public static boolean isToday(Date date) {
        return isSameDay(date, new Date());
	}
	
	private static Date parseDateWithLeniency(
            String str, String[] parsePatterns, boolean lenient) throws ParseException {
        if (str == null || parsePatterns == null) {
            throw new IllegalArgumentException("Date and Patterns must not be null");
        }
        
        SimpleDateFormat parser = new SimpleDateFormat();
        parser.setLenient(lenient);
        ParsePosition pos = new ParsePosition(0);
        for (String parsePattern : parsePatterns) {

            String pattern = parsePattern;

            // LANG-530 - need to make sure 'ZZ' output doesn't get passed to SimpleDateFormat
            if (parsePattern.endsWith("ZZ")) {
                pattern = pattern.substring(0, pattern.length() - 1);
            }
            
            parser.applyPattern(pattern);
            pos.setIndex(0);

            String str2 = str;
            // LANG-530 - need to make sure 'ZZ' output doesn't hit SimpleDateFormat as it will ParseException
            if (parsePattern.endsWith("ZZ")) {
                str2 = str.replaceAll("([-+][0-9][0-9]):([0-9][0-9])$", "$1$2"); 
            }

            Date date = parser.parse(str2, pos);
            if (date != null && pos.getIndex() == str2.length()) {
                return date;
            }
        }
        throw new ParseException("Unable to parse the date: " + str, -1);
    }
	
	public static Date parseDate(String str, String... parsePatterns) throws ParseException {
        return parseDateWithLeniency(str, parsePatterns, true);
    }
	
	/**
	 * 将时间字符串从一个格式转换成另一个格式
	 * @param dateStr 时间字符串
	 * @param sourcePattern 原来的格式
	 * @param targetPattern 目标格式
	 * @return
	 */
	public static String changeDateString(String dateStr, String sourcePattern, String targetPattern){
		try {
			Date date = parseDate(dateStr, sourcePattern);
			return date2string(date, targetPattern);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	public static void main(String[] args) {
       // System.out.println(isToday("2016-10-28 10:10:10"));
        //System.out.println(isToday("20161028101010"));
       // System.out.println(isToday("20161028 10:10:10"));
        //System.out.println(isToday("2016-10-28 10-10-10"));
        System.out.println(changeDateString("20161028101014","yyyyMMddHHmmss", "yyyyMMdd HH:mm:ss"));
        
    }
}
